/// 聊天消息数据模型
/// 包含消息的基本信息和类型
class ChatMessageEntity {
  final String messageId;
  final String conversationId;
  final String senderId;
  final String receiverId;
  final ChatMessageType messageType;
  final String content;
  final DateTime timestamp;

  const ChatMessageEntity({
    required this.messageId,
    required this.conversationId,
    required this.senderId,
    required this.receiverId,
    required this.messageType,
    required this.content,
    required this.timestamp,
  });

  /// 从JSON创建消息对象
  factory ChatMessageEntity.fromJson(Map<String, dynamic> json) {
    return ChatMessageEntity(
      messageId: json['messageId'] as String,
      conversationId: json['conversationId'] as String,
      senderId: json['senderId'] as String,
      receiverId: json['receiverId'] as String,
      messageType: ChatMessageType.values.firstWhere(
        (e) => e.toString() == 'ChatMessageType.${json['messageType']}',
      ),
      content: json['content'] as String,
      timestamp: DateTime.parse(json['timestamp'] as String),
    );
  }

  /// 转换为JSON
  Map<String, dynamic> toJson() {
    return {
      'messageId': messageId,
      'conversationId': conversationId,
      'senderId': senderId,
      'receiverId': receiverId,
      'messageType': messageType.toString().split('.').last,
      'content': content,
      'timestamp': timestamp.toIso8601String(),
    };
  }

  /// 复制消息对象并修改部分属性
  ChatMessageEntity copyWith({
    String? messageId,
    String? conversationId,
    String? senderId,
    String? receiverId,
    ChatMessageType? messageType,
    String? content,
    DateTime? timestamp,
  }) {
    return ChatMessageEntity(
      messageId: messageId ?? this.messageId,
      conversationId: conversationId ?? this.conversationId,
      senderId: senderId ?? this.senderId,
      receiverId: receiverId ?? this.receiverId,
      messageType: messageType ?? this.messageType,
      content: content ?? this.content,
      timestamp: timestamp ?? this.timestamp,
    );
  }

  @override
  String toString() {
    return 'ChatMessageEntity(messageId: $messageId, senderId: $senderId, messageType: $messageType, content: $content)';
  }

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;
    return other is ChatMessageEntity && other.messageId == messageId;
  }

  @override
  int get hashCode => messageId.hashCode;
}

/// 聊天消息类型枚举
enum ChatMessageType {
  textMessage, // 文本消息
  callFailedMessage, // 拨打失败消息
}
